// Product.java
package org.example.model;

import java.math.BigDecimal;
import org.example.exception.InvalidNameException;
import org.example.exception.InvalidPriceException;
import org.example.exception.InventoryUnderflowException;

public abstract class Product {
    private String id;
    private String name;
    private BigDecimal price;
    private int quantity;

    public Product(String id, String name, BigDecimal price, int quantity) {
        setId(id);
        setName(name);
        setPrice(price);
        setQuantity(quantity);
    }

    // Getter和Setter方法
    public String getId() { return id; }

    public void setId(String id) {
        if (id == null || id.trim().isEmpty()) {
            throw new IllegalArgumentException("商品ID不能为空");
        }
        this.id = id;
    }

    public String getName() { return name; }

    public void setName(String name) {
        if (name == null || name.trim().isEmpty()) {
            throw new InvalidNameException("商品名称不能为空");
        }
        if (name.length() > 50) {
            throw new InvalidNameException("商品名称不能超过50个字符");
        }
        this.name = name;
    }

    public BigDecimal getPrice() { return price; }

    public void setPrice(BigDecimal price) {
        if (price.compareTo(BigDecimal.ZERO) <= 0) {
            throw new InvalidPriceException("价格必须大于0");
        }
        this.price = price;
    }

    public int getQuantity() { return quantity; }

    public void setQuantity(int quantity) {
        if (quantity < 0) {
            throw new InventoryUnderflowException("库存不能为负数");
        }
        this.quantity = quantity;
    }

    public abstract void displayInfo();
}
